package com.zhentao.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zhentao.common.enums.admin.AdActivity;
import com.zhentao.common.enums.dto.AdActivityDto;
import com.zhentao.dtos.ResponseResult;
import com.zhentao.dtos.Result;
import com.zhentao.helper.ThreadLocalUtil;
import com.zhentao.mapper.AdActivityMapper;
import com.zhentao.service.AdActivityService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
public class AdActivityServiceimpl extends ServiceImpl<AdActivityMapper, AdActivity>
        implements AdActivityService {
    @Autowired
    private AdActivityMapper adActivityMapper;
    @Autowired
    private FileStorageServiceImpl fileStorageService;
    @Override
    public ResponseResult acrivitylist(AdActivityDto adActivityDto) {
        List<AdActivity> actinvitylist = adActivityMapper.actinvitylist(adActivityDto);
        return ResponseResult.okResult(actinvitylist);
    }



    @Override
    public Result activityList(AdActivityDto adActivityDto) {
        Page<AdActivity> adActivityPage = new Page<>(adActivityDto.getPageNum(), adActivityDto.getPageSize());
        LambdaQueryWrapper<AdActivity> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(adActivityDto.getTitle()), AdActivity::getTitle, adActivityDto.getTitle())
                .like(StringUtils.isNotBlank(adActivityDto.getAddress()),AdActivity::getAddress, adActivityDto.getAddress())
                .eq(adActivityDto.getStatus() != null, AdActivity::getStatus, adActivityDto.getStatus())
                .orderByDesc(AdActivity::getCreateTime);
        adActivityPage = adActivityMapper.selectPage(adActivityPage, wrapper);
        return Result.success("操作成功").put("total", adActivityPage.getTotal()).put("data", adActivityPage.getRecords());
    }

    @Override
    public Result activitySave(AdActivity adActivity) {

        LambdaUpdateWrapper<AdActivity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(adActivity.getTitle()),AdActivity::getTitle, adActivity.getTitle());
        AdActivity adActivity1 = adActivityMapper.selectOne(wrapper);

        if (adActivity.getActivityId() == null){
            if (adActivity1 != null ){
                return Result.error("此活动不能重复，请修改活动标题");
            }
            adActivity.setCreateTime(new Date());
            adActivity.setStatus(0);
//            adActivity.setCreateBy(Integer.valueOf(ThreadLocalUtil.get().toString()));
        }else {
//            adActivity.setUpdateBy(Integer.valueOf(ThreadLocalUtil.get().toString()));
            adActivity.setUpdateTime(new Date());
        }

        boolean b = this.saveOrUpdate(adActivity);
        if (b){
            return Result.success("操作成功");
        }else {
            return Result.error("操作失败");
        }
    }

    @Override
    public Result activityUpd(AdActivity adActivity) {

        if (adActivity.getActivityId() == null || adActivity.getActivityId() <= 0){
            return Result.error("请选择要操作的数据");
        }

        LambdaUpdateWrapper<AdActivity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(adActivity.getTitle()),AdActivity::getTitle, adActivity.getTitle());
        AdActivity adActivity1 = adActivityMapper.selectOne(wrapper);
        if (adActivity1 != null && adActivity1.getActivityId()!=adActivity.getActivityId()) {
            return Result.error("不能重复发布");
        }

        adActivity.setUpdateTime(new Date());
//        adActivity.setUpdateBy(Integer.valueOf(ThreadLocalUtil.get().toString()));
        int i = adActivityMapper.updateById(adActivity);
        if (i == 1){
            return Result.success("操作成功");
        }else{
            return Result.error("操作失败");
        }
    }

    @Scheduled(cron = "0 0/30 0 * * ?")
    public void updateStatusIfRequired(){
        Date date = new Date();
        List<AdActivity> list = this.list();
        for (AdActivity adActivity : list) {
            if (adActivity.getStartDate() != null && adActivity.getStartDate().before(new Date())){
                adActivity.setStatus(1);
                this.updateById(adActivity);
            }
        }
    }
    @Scheduled(cron = "0 0/30 0 * * ?")
    public void updateStatusIfEnd(){
        Date date = new Date();
        List<AdActivity> list = this.list();
        for (AdActivity adActivity : list) {
            if (adActivity.getEndDate() != null && adActivity.getEndDate().before(new Date())){
                adActivity.setStatus(2);
                this.updateById(adActivity);
            }
        }
    }

//    @Override
//    public Result activityDelete(Integer activityId) {
//
//        if (activityId == null || activityId <= 0){
//            return Result.error("请选择要删除数据");
//        }
//        int i = adActivityMapper.deleteById(activityId);
//        if (i == 1){
//            return Result.success("操作成功");
//        }else {
//            return Result.error("操作失败");
//        }
//    }

    @Override
    public Result upload(MultipartFile file) throws IOException {
        return Result.success(fileStorageService.uploadPicFile(file.getOriginalFilename(), file.getInputStream()));
    }

    @Override
    public Result activityDelete(Integer activityId) {

        return Result.success("操作成功");
    }
}
